package com.tangyee.home_blood_center.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.view.BPRecordRowView;
import com.tangyee.home_blood_center.view.CircleView;
import com.tangyee.R;
import com.tangyee.home_blood_center.bean.BSRecord;
import com.tangyee.home_blood_center.db.BSRecordHolder;
import com.tangyee.home_blood_center.interfaces.OnBPRecordClickListener;
import com.tangyee.home_blood_center.view.BPRecordRowView;
import com.tangyee.home_blood_center.view.CircleView;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示血压信息表格的fragment
 */
public class BloodPressureFragment extends  BaseFragment{
    private List<BSRecord> mBSRecords; // 展示的记录
    private List<BPRecordRowView> mBPRecordRowViews; // 自已的展示一行血压数据的view
    private CircleView mCircleView1; // 圆形view1 展示偏高数据信息
    private CircleView mCircleView2; // 圆形view2 展示正常数据信息
    private CircleView mCircleView3; // 圆形view3 展示偏低数据信息
    private int low; // 偏低的数据数
    private int good; // 正常的数据数
    private int high; // 偏高的数据数

    /**
     * 创建本类实例
     * @param text
     * @return
     */
    public static BloodPressureFragment newInstance(String text){
        Bundle bundle = new Bundle();
        bundle.putString("text",text);
        BloodPressureFragment bloodPressureFragment = new BloodPressureFragment();
        bloodPressureFragment.setArguments(bundle);
        return  bloodPressureFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blood_pressure_list, container, false);

        LinearLayout linearLayout = view.findViewById(R.id.BP_record_table);
        mBPRecordRowViews = new ArrayList<>();
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(30);
        int n = mBSRecords.size();
        low = good = high = 0;
        for (int i = 0; i < n; i ++) {
            // 统计信息
            BSRecord record = mBSRecords.get(i);
            analyzeData(90, 140, record.getSystolicBP());
            analyzeData(60, 80, record.getDiastolicBP());

            // 构建BSRecordRow
            BPRecordRowView bpRecordRowView = new BPRecordRowView(getContext());
            bpRecordRowView.setBSRecord(mBSRecords.get(i));
            bpRecordRowView.setOnBPRecordClickListener(new OnBPRecordClickListener() {
                @Override
                public void OnTextView1Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 1;
                    startAddDataFragment(sign);
                }

                @Override
                public void OnTextView2Click(BSRecord bsRecord) {
                    Long sign = bsRecord.getDate().getTime() * 10 + 2;
                    startAddDataFragment(sign);
                }

            });
            mBPRecordRowViews.add(bpRecordRowView);
            linearLayout.addView(bpRecordRowView, 0);
        }

        mCircleView1 = view.findViewById(R.id.BP_info_left);
        mCircleView2 = view.findViewById(R.id.BP_info_middle);
        mCircleView3 = view.findViewById(R.id.BP_info_right);
        int total = low + good + high;
        mCircleView1.setDate(low, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(high, total);

        return view;
    }

    /**
     * 分析数据，判断数据是偏高正常还是偏低
     * @param low
     * @param high
     * @param data
     */
    private void analyzeData(int low, int high, int data) {
        // TODO: 2018/8/29 根据BPRecordRowView来分析数据
        if (data <= 0) {
            return;
        }
        if (data < low) {
            this.low ++;
            return;
        }
        if (data > high) {
            this.high ++;
            return;
        }
        good ++;
    }


    /**
     * 启动添加数据的fragment
     * @param sign
     */
    private void startAddDataFragment(Long sign) {
        // TODO: 2018/8/29 添加数据的fragment
        AddBloodPressureRecordFragment addBloodPressureRecordFragment = AddBloodPressureRecordFragment.newInstance(sign);
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        List<Fragment> fragments = fm.getFragments();
        transaction.hide(fragments.get(0));
        transaction.setCustomAnimations(R.anim.move_right_in_activity, R.anim.move_right_out_activity, R.anim.move_right_out_activity, R.anim.move_left_out_activity) // 瞎设置
                .add(R.id.fragment_container, addBloodPressureRecordFragment, "addBloodPressureRecordFragment")
                .addToBackStack("addBloodPressureRecordFragment")
                .commit();
    }

    /**
     * 重写setUserVisibleHint方法，用于输入数据完成后更新界面
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            updateUI();
        } else {
            //相当于Fragment的onPause
        }
    }

    /**
     * 也是用于输入数据后更新界面
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        updateUI();
    }

    /**
     * 更新UI
     */
    private void updateUI() {
        if (mBPRecordRowViews.size() == 0) {
            return;
        }
        low = high = good = 0;
        mBSRecords = BSRecordHolder.get(getContext()).getBSRecords(30);
        for (int i = 0; i < mBSRecords.size(); i ++) {
            // 统计信息
            final BSRecord record = mBSRecords.get(i);
            analyzeData(90, 140, record.getSystolicBP());
            analyzeData(60, 80, record.getDiastolicBP());

            // 重设数据
            mBPRecordRowViews.get(i).setBSRecord(record);
        }
        int total = low + good + high;
        mCircleView1.setDate(high, total);
        mCircleView2.setDate(good, total);
        mCircleView3.setDate(low, total);
    }

}
